package com.ai.javaailangchain4j.config;

import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.pinecone.PineconeEmbeddingStore;
import dev.langchain4j.store.embedding.pinecone.PineconeIndexConfig;
import dev.langchain4j.store.embedding.pinecone.PineconeServerlessIndexConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EmbeddingStoreConfig {

    @Autowired
    private EmbeddingModel embeddingModel;

    /**
     * 配置向量存储对象
     * @return
     */
    @Bean
    public EmbeddingStore<TextSegment> embeddingStore() {
//        创建向量存储
        String apiKey = System.getenv("PCE_STORE_API_KEY");
        if (apiKey == null || apiKey.isEmpty()) {
            throw new IllegalStateException("PCE_STORE_API_KEY environment variable is not set");
        }
     return   PineconeEmbeddingStore.builder()
                .apiKey(apiKey)
                .index("pinecone-test1")  //向量索引名称
                .nameSpace("pinecone-test_namespace")//向量空间名称
             .createIndex(
                     PineconeServerlessIndexConfig.builder()
                             .cloud("AWS")//指定索引部署在AWS云服务上
                             .region("us-east-1")//指定索引部署在哪个区域
                             .dimension(embeddingModel.dimension())//指定索引的维度  ,该维度与embeddingModel生成的向量维度一致
                             .build())
                .build();

    }
}
